#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    snakeHead.setX(300); //初始化蛇的位置
    snakeHead.setY(300);
    snakeList.push_back(snakeHead); //将蛇头存入链表
    startTimer(100);    //初始化移动速度
    foodList.push_back(QPoint(100,100));//初始食物
    isEat=false;
}

MainWindow::~MainWindow()
{
    delete ui;
}

//重写绘画事件
void MainWindow::paintEvent(QPaintEvent *event)
{
    //定义一个画家
    QPainter painter(this);// 一定要加这个this
    //定义一个画笔
    QPen pen(Qt::gray);
    pen.setWidth(1);      //设置笔的粗细
    //定义一个笔刷
    QBrush brush(Qt::green);
    //使用画家
    painter.setPen(pen);
    painter.setBrush(brush);

    //绘制竖线，X不变
    for (int i = 0; i <rect().width()/grid_side; ++i)
    {
        painter.drawLine(QPoint(i*grid_side,0),QPoint(i*grid_side,rect().height()*grid_side));
    }
    //绘制横线,Y值不变
    for (int i = 0; i <rect().height()/grid_side; ++i)
    {
        painter.drawLine(QPoint(0,i*grid_side),QPoint(rect().height()*grid_side,i*grid_side));
    }


    //绘制食物
    for (QList<QPoint>::iterator it=foodList.begin(); it!=foodList.end(); it++)
    {
        painter.drawEllipse(it->x(),it->y(),grid_side,grid_side);
    }

    //绘制贪吃蛇
    for (int i = 0; i < snakeList.size(); ++i)
    {
        painter.drawEllipse(snakeList[i].x(),snakeList[i].y(),grid_side,grid_side);
    }

}

//重写键盘响应事件
void MainWindow::keyPressEvent(QKeyEvent *event)
{
    switch (event->key())
    {
    //上
    case Qt::Key_Up:
        if(direction!=2)
        {
            direction=1;
        }
        break;
     //下
    case Qt::Key_Down:
        if(direction!=1)
        {
            direction=2;
        }
        break;
     //左
    case Qt::Key_Left:
        if(direction!=4)
        {
            direction=3;
        }
        break;
     //右
    case Qt::Key_Right:
        if(direction!=3)
        {
            direction=4;
        }
        break;
    default:
        break;
    }
}
//重写时间事件
void MainWindow::timerEvent(QTimerEvent *event)
{
    //将后一个点的坐标等于前一个点的坐标，让蛇动起来
    for (int i = snakeList.size()-1; i>0; i--)
    {
        //判断蛇身是否重叠
        if(snakeList[0]!=snakeList[i])
        {
            snakeList[i]=snakeList[i-1];
        }
    }
    //移动蛇头的位置
    switch (direction)
    {
    case 1:   //上移
        snakeList[0].setY(snakeList[0].y()-grid_side);
        for (int i = 1; i < snakeList.size(); ++i)
        {
            if(snakeList[0].y()<0||snakeList[0]==snakeList[i])
            {
                QMessageBox::warning(this,"游戏结束","撞到墙或者自己了!");
                exit(0);
            }
        }
        break;
    case 2:   //下移
        snakeList[0].setY(snakeList[0].y()+grid_side);
        for (int i = 1; i < snakeList.size(); ++i)
        {
            if(snakeList[0].y()==rect().height()||snakeList[0]==snakeList[i])
            {
                QMessageBox::warning(this,"游戏结束","撞到墙或者自己了!");
                exit(0);
            }
        }
        break;
    case 3:   //左移
        snakeList[0].setX(snakeList[0].x()-grid_side);
        for (int i = 1; i < snakeList.size(); ++i)
        {
            if(snakeList[0].x()<0||snakeList[0]==snakeList[i])
            {
                QMessageBox::warning(this,"游戏结束","撞到墙或者自己了!");
                exit(0);
            }
        }
        break;
    case 4:   //右移
        snakeList[0].setX(snakeList[0].x()+grid_side);
        for (int i = 1; i < snakeList.size(); ++i)
        {
            if(snakeList[0].x()==rect().width()||snakeList[0]==snakeList[i])
            {
                QMessageBox::warning(this,"游戏结束","撞到墙或者自己了!");
                exit(0);
            }
        }
        break;
    default:
        break;  
    }


    for (QList<QPoint>::iterator it=foodList.begin(); it!=foodList.end(); it++)
    {
        //判断食物是否被蛇吃
        if(snakeList[0]==*it)
        {
            QPoint pt=*it;
            snakeList.push_back(pt);//吃到后蛇就长大
            foodList.erase(it);//食物减少
            isEat=true;
        }
    }

    if(isEat)
    {
        //重新生成食物
        qsrand(static_cast<unsigned int>(time(nullptr)));//初始化随机数
        int x=qrand()%(rect().width()/20)*20;           //随机生成食物位置
        int y=qrand()%(rect().height()/20)*20;
        foodList.push_back(QPoint(x,y));               //存放每个点的食物
        isEat=false;//每次只生成一个食物
    }
    repaint();//重绘
}


